#include <vector>
#include <iostream>
#include <unordered_set>

using namespace std;

class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_set<int> window;
        if (k + 1 >= nums.size()) {
            for (int it : nums) {
                if (window.count(it) == 1) {
                    return true;
                }
                window.insert(it);
            }
            return false;
        }
        for (int i = 0; i <= k; i++) {
            if (window.count(nums[i]) == 1) {
                return true;
            }
            window.insert(nums[i]);
        }
        for (int i = k + 1; i < nums.size(); i++) {
            window.erase(nums[i - k - 1]);
            if (window.count(nums[i]) == 1) {
                return true;
            }
            window.insert(nums[i]);
        }
        return false;
    }
};

int main() {
    vector<int> input = {1, 2, 3, 1, 2, 3};
    Solution solution;
    bool output = solution.containsNearbyDuplicate(input, 2);
    cout << output << endl;
    return 0;
}